package com.lagou.test04.test05;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class SocketTest2 {

    public static void main(String[] args) {
        Socket socket = null;
        PrintStream stream = null;
        Scanner sc = null;
        BufferedReader bf = null;
        try {
            socket = new Socket("192.168.1.103",8889);
            Thread.sleep(1000);
            System.out.println("连接服务器成功！");

            stream = new PrintStream(socket.getOutputStream());
            sc = new Scanner(System.in);
            while (true){
                System.out.println("选择发送类型，文字输入选1，文件传输选2,输入bye结束：");
                String str1 = sc.next();

                if ("bye".equalsIgnoreCase(str1)) {
                    System.out.println("聊天结束！");
                    stream.println(str1);
                    break;
                }

                if ("1".equalsIgnoreCase(str1)) {
                    System.out.println("输入聊天内容：");
                    str1 = sc.next();
                    stream.println(str1);
                }

                if ("2".equalsIgnoreCase(str1)) {
                    System.out.println("输入文件路径：");
                    String path = sc.next();
                    stream.println(path);
                }

                bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String s1 =  bf.readLine();
                System.out.println("服务器端返回消息为--->"+s1);

            }

            //stream.println("Hello!");

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {

            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


            if (stream != null) {
                stream.close();
            }

        }



    }
}
